const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  let week = date.getDay();
  week =
    week == 1
      ? "一"
      : week == 2
      ? "二"
      : week == 3
      ? "三"
      : week == 4
      ? "四"
      : week == 5
      ? "五"
      : week == 6
      ? "六"
      : "日";

  return {
    year,
    month,
    day,
    hour,
    minute,
    second,
    week,
  };
};

const formatDates = (date) => {
  const month = date.getMonth() + 1;
  return [month];
};


// 获取时间戳 (本月第一天00.00.00  本月最后一天23.59.59)
const getFirstAndLastDay = () => {
  const now = new Date();
  const startTime = new Date(now.getFullYear(), now.getMonth(), 1).getTime(); // 本月第一天 00:00:00
  const endTime = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59).getTime(); // 本月最后一天 23:59:59
  return {
    startTime,
    endTime,
  };
};


// 获取当天开始时间结束时间(传参)
const getStartTimeAndEndTime = (time) => {
  const date = new Date(time);
  // 设置时间为当天的0点0分0秒
  date.setHours(0, 0, 0, 0);
  // 获取当天的开始时间（即当天的0点0分0秒），并转换为Unix时间戳
  const startTime = date.getTime() / 1000;
  // 设置时间为当天的23点59分59秒
  date.setHours(23, 59, 59, 999);
  // 获取当天的结束时间（即当天的23点59分59秒），并转换为Unix时间戳,将结束时间四舍五入到整数部分
  const endTime = Math.round(date.getTime() / 1000);
  return {
    startTime,
    endTime,
  }
};

// 获取当天开始时间结束时间
const getDayStartAndEndTimestamp = (date = new Date()) => {
  const startTime = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime() / 1000;
  const endTime = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59).getTime() / 1000;
  return { startTime, endTime };
}

module.exports = {
  formatTime: formatTime,
  formatDates,
  getFirstAndLastDay,
  getStartTimeAndEndTime,
  getDayStartAndEndTimestamp
};
